package Servlets;

import java.io.IOException;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Beans.Movie;
import ServiceInterface.ServiceProxy;

/**
 * Servlet implementation class Menu
 */
@WebServlet("/Menu")
public class Menu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	ServiceProxy proxy = new ServiceProxy();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Menu() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		
		try {
			String unsubscribe = request.getParameter("unsubscribe");
			String deleteMovie = request.getParameter("deleteMovie");
			String rentMovie = request.getParameter("rentMovie");
			String returnMovie = request.getParameter("returnMovie");
			String listPremiumMembers = request.getParameter("listPremiumMembers");
			String listSimpleMembers = request.getParameter("listSimpleMembers");
			String listMovies = request.getParameter("listMovies");
			String listPersons = request.getParameter("listPersons");
			String changePerson = request.getParameter("changePerson");
			String changeMovie = request.getParameter("changeMovie");
			String searchPerson = request.getParameter("searchPerson");
			String searchMovie = request.getParameter("searchMovie");
			String displayPerson = request.getParameter("displayPerson");
			String displayMovie = request.getParameter("displayMovie");
			String generateBill = request.getParameter("generateBill");
			String signOff = request.getParameter("signOff");
			
			if (unsubscribe != null) {
				HttpSession session = request.getSession(true);
				
				if (session != null) {
					response.sendRedirect("Unsubscribe.jsp");
					
				} else {
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (deleteMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("DeleteMovie.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (rentMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("RentMovie.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (returnMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ReturnMovie.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (listPremiumMembers != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ListPremiumMembers.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (listSimpleMembers != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ListSimpleMembers.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (listMovies != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ListMovies.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (listPersons != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ListPersons.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (changePerson != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ChangePerson.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (changeMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("ChangeMovie.jsp");
				} else {
					
					ServletContext sc = getServletContext();
					RequestDispatcher rd = sc.getRequestDispatcher("/View/SignIn.jsp");
					rd.forward(request, response);
				}
			}
			else if (searchPerson != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("SearchPerson.jsp");
				} else {
					
					ServletContext sc = getServletContext();
					RequestDispatcher rd = sc.getRequestDispatcher("/View/SignIn.jsp");
					rd.forward(request, response);
				}
			}
			else if (searchMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("SearchMovie.jsp");
				} else {
					
					ServletContext sc = getServletContext();
					RequestDispatcher rd = sc.getRequestDispatcher("/View/SignIn.jsp");
					rd.forward(request, response);
				}
			}
			else if (displayPerson != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("DisplayPerson.jsp");
				} else {
					
					ServletContext sc = getServletContext();
					RequestDispatcher rd = sc.getRequestDispatcher("/View/SignIn.jsp");
					rd.forward(request, response);
				}
			}
			else if (displayMovie != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("DisplayMovie.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (generateBill != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("GenerateBill.jsp");
				} else {
					
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}
			else if (signOff != null) {
				HttpSession session = request.getSession(false);
				
				if (session != null) {
					String email = (String) session.getAttribute("email");

					response.sendRedirect("Login.jsp");
				} else {
					getServletContext().getRequestDispatcher("/View/SignIn.jsp").forward(request, response);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
